define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'csmitsm_xcore', 'csmitsm_xcflow'], function ($, undefined, Backend, Table, Form, xcore, xcflow) {


    function getColumn(table) {
        var column = [{
                checkbox: true
            },
            {
                field: 'id',
                title: __('Id')
            },
            {
                field: 'flowtitle',
                title: '流程名称',
                operate: 'LIKE'
            },
            {
                field: 'createtime',
                title: '流程发起时间',
                operate: 'RANGE',
                addclass: 'datetimerange',
                autocomplete: false,
                formatter: Table.api.formatter.datetime
            },
            {
                field: 'xcflow_fstatusname',
                title: '当前节点',
                operate: 'LIKE'
            },
            {
                field: 'xcflow_toaudit_displayname',
                title: '待处理人',
            },
            {
                field: 'status',
                title: __('Status'),
                searchList: {
                    "normal": __('Normal'),
                    "hidden": __('Hidden')
                },
                formatter: Table.api.formatter.status
            },
            {
                field: 'operate',
                width: "150px",
                title: __('Operate'),
                table: table,
                buttons: [{
                    // name: 'addtabs',
                    // title: __('流程详情'),
                    // classname: 'btn btn-xs btn-warning btn-addtabs',
                    // icon: 'fa fa-leaf',
                    // url: 'csmitsm/xcmyflowinstance/showXcflowPage?1=1'
                    name: 'click',
                    title: function (row) {
                        return row.name;
                    },
                    classname: 'btn btn-xs btn-warning btn-click',
                    icon: 'fa fa-leaf',
                    click: function (data, row) {
                        xcore.openWindow(Fast.api.fixurl('csmitsm/xcmyflowinstance/showXcflowPage?1=1&ids=' + row.id) + "&dialog=1");
                    }
                }],
                formatter: Table.api.formatter.operate
            },
        ];
        return column;

    }

    var Controller = {
        allflows: function () {
            var that = this;
            Fast.config.openArea = ['98%', '98%'];
            $(".btn-createOrUpdateXcformPage").click(function () {
                xcflow.xcformcrud_openCreateOrUpdateXcformPage(true, "csmitsm/xcmyflowinstance/", $(this).data("id"), "");
            });
        },
        index: function () {
            if(Config.open_xcflowinstance_id!=null && Config.open_xcflowinstance_id!=""){
                var uu = Fast.api.fixurl("csmitsm/xcmyflowinstance/showXcflowPage?1=1&ids="+Config.open_xcflowinstance_id);
                Fast.api.open(uu,"流程处理");
            }   
            

            Fast.config.openArea = ['98%', '98%'];

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    table: 'csmitsm_xcflow_instance',
                }
            });

            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.length > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            daiban: function () {
                var table = $("#tabledaiban");
                table.bootstrapTable({
                    url: 'csmitsm/xcmyflowinstance/index_daiban',
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    search: false,
                    columns: [getColumn(table)]
                });
                Table.api.bindevent(table);
            },
            mycreate: function () {
                var table = $("#tablemycreate");
                table.bootstrapTable({
                    url: 'csmitsm/xcmyflowinstance/index_mycreate',
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    search: false,
                    columns: [getColumn(table)]
                });
                Table.api.bindevent(table);
            },
            myaudit: function () {
                var table = $("#tablemyaudit");
                table.bootstrapTable({
                    url: 'csmitsm/xcmyflowinstance/index_myaudit',
                    toolbar: '#toolbar3',
                    sortName: 'id',
                    search: false,
                    columns: [getColumn(table)]
                });
                Table.api.bindevent(table);
            }
        },
        createorupdatexcformpage: function () {
            var that = this;
            var baseurl = "csmitsm/xcmyflowinstance/";
            new Vue({
                el: '#app',
                data: function () {
                    return {
                        fieldfulljson: Config.fieldfulljson,
                        xcform: Config.xcform,
                        flowinstance: Config.flowinstance,
                        xcfieldtags: Config.xcfieldtags,
                        xcflowinstancetraces: Config.xcflowinstancetraces,
                        hasSaveOrSubmitBtn: Config.hasSaveOrSubmitBtn,
                        control: {
                            form: {}
                        }
                    };
                },
                methods: {
                    control_clickSave: function () {
                        var that = this;
                        var data = xcflow.xcformcrud_getFormData(that.fieldfulljson);
                        xcflow.xcformcrud_saveFormData(baseurl, data, function () {
                            return true;
                        });
                    },
                    control_clickSubmit: function () {
                        var that = this;
                        var data = xcflow.xcformcrud_getFormData(that.fieldfulljson);
                        that.$refs["form"].validate((valid) => {
                            if (valid) {
                                xcflow.xcformcrud_submitFormData(baseurl, data, function () {
                                    return true;
                                });
                            }
                        });
                    },
                    control_clickDelete: function () {
                        var that = this;
                       
                        Fast.api.ajax({
                            url: Fast.api.fixurl(baseurl + "del/ids/"+Config.id),
                            type: "get",
                            data: {
                                xcform_id: Config.xcform.id,
                            },
                        }, function (res) {
                            alert('数据删除成功');
                            xcflow.xcformcrud_openCreateOrUpdateXcformPage(false, baseurl, Config.xcform.id, "");
                            return false;
                        });
                    }
                }
            });
        },
        showxcformpage: function () {
            var that = this;
            var baseurl = "csmitsm/xcmyflowinstance/";
            new Vue({
                el: '#app',
                data: function () {
                    return {
                        fieldfulljson: Config.fieldfulljson,
                        editXcfieldfulljson: Config.editXcfieldfulljson,
                        xcform: Config.xcform,
                        flowinstance: Config.flowinstance,
                        xcflowinstancetraces: Config.xcflowinstancetraces,
                        hasFlowButton: Config.hasFlowButton,
                        logicObj: Config.logicObj,
                        fstatusObj: Config.fstatusObj,
                        control: {
                            form: {},
                            dialog_pass_visible: false,
                            dialog_pass_content: '',
                            dialog_reject_visible: false,
                            dialog_reject_content: '',
                            dialog_title: '',
                        }
                    };
                },
                methods: {
                    control_clickLogicButton: function (btnType, btnName) {
                        var that = this;
                        that.control.dialog_title = btnName;
                        switch (btnType) {
                            case "pass":
                                that.control.dialog_pass_visible = true;
                                break;
                            case "reject":
                                that.control.dialog_reject_visible = true;
                                break;
                        }
                    },
                    control_clickReject: function () {
                        var that = this;
                        if (that.control.dialog_reject_content == '') {
                            that.control.dialog_reject_content = "不同意";
                        }

                        var data = xcflow.xcformcrud_getFormData(that.editXcfieldfulljson);
                        data['tracecontent'] = that.control.dialog_reject_content;

                        Fast.api.ajax({
                            url: Fast.api.fixurl(baseurl + "submitRejectFlowAjax"),
                            type: "post",
                            data: data,
                        }, function (res) {
                            xcflow.xcformcrud_openShowXcformPage(false, "csmitsm/xcmyflowinstance/", Config.xcform.id, Config.id);
                            return true;
                        });
                    },
                    control_clickPass: function () {
                        var that = this;
                        if (that.control.dialog_pass_content == '') {
                            that.control.dialog_pass_content = "同意";
                        }
                        var data = xcflow.xcformcrud_getFormData(that.editXcfieldfulljson);
                        data['tracecontent'] = that.control.dialog_pass_content;

                        that.$refs["passform"].validate((valid) => {
                            if (valid) {
                                Fast.api.ajax({
                                    url: Fast.api.fixurl(baseurl + "submitPassFlowAjax"),
                                    type: "post",
                                    data: data,
                                }, function (res) {
                                    xcflow.xcformcrud_openShowXcformPage(false, "csmitsm/xcmyflowinstance/", Config.xcform.id, Config.id);
                                    return true;
                                });
                            }
                        });
                    },
                }
            });
        },
    };
    return Controller;
});